<template lang="pug">
mt-dialog(
  v-model="appmodel"
  title="标本排样"
  :on-close="updateModel"
  :on-cancel="updateModel"
  hide-confirm
  size="large"
)
  .sampleAeeangement
    .sampleAeeangement__left
      SectionTitle 标本信息
      mt-form(
      v-bind="editFormProps"
        )
    .sampleAeeangement__center
      SectionTitle 申请项目信息
      mt-table(
      v-bind="projectTableProps"
      )
    .sampleAeeangement__right
      SectionTitle 已排标本
      mt-table(
      v-bind="tableProps"
    )

</template>

<script setup lang="ts">
import { type QueryPagination, useApi } from "@scmt/core-api";
import { useQuery } from "@scmt/common-hooks";
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type TableProps,
  type EditFormProps,
} from "@scmt/common-components";
import { type SpecimenQueryParams } from "@scmt/api-lis-specimen";
import { SectionTitle } from "@scmt/common-components";
//添加 Props属性
const $api = useApi();
const editingObj = ref<{} | Object>({});
const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const queryPramas = reactive<SpecimenQueryParams>({
  pageNum: 1,
  pageSize: 20,
});
const updateModel = () => {
  appmodel.value = false;
};

const { query, data, loadingMap } = useQuery({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.lis.specimen.query(params).then((res) => {
      emit("message-to-parent", res);
      return res;
    });
  },
  listDoctorAsOptions: (val: string) =>
    $api.user.listDoctorAsOptions({ officeId: val }),

  listAsOptions: (val: string) => $api.upms.org.listAsOptions(),
  listAsBedOptions: () => $api.osm.bed.listAsOptions(),
});

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "reason",
      label: "标本号:",
      required: false,
    },
    {
      prop: "remark",
      label: "检验仪器",
      required: false,
      as: "select",
      options: {
        loadMethod: () => $api.lis.instrument.listAsOptions(),
      },
    },
    {
      prop: "remark",
      label: "检验日期",
      required: false,
      as: "date",
    },
    {
      prop: "remark",
      label: "扫描条码号",
      required: false,
    },
    {
      prop: "remark",
      label: "",
      required: false,
      as: "checkbox-group",
      style: {
        marginLeft: "80px",
      },
      options: {
        loadMethod: () => {
          return [
            {
              label: "扫描并自动签收",
              value: 1,
            },
          ];
        },
      },
    },
    {
      prop: "remark",
      label: "",
      required: false,
      span: 24,
      render: () => {
        return h(
          resolveComponent("mt-button"),
          {
            type: "primary",
            onClick: () => {},
            style: {
              marginLeft: "80px",
            },
          },
          {
            default: () => "确定",
          }
        );
      },
    },
  ];

  const editProps: EditFormProps = {
    items: formItems,
    model: editingObj.value,
    columns: 1,
  };
  return editProps;
});

const projectTableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    insertIndex: false,
    columns: [
      {
        prop: "specimenNumber",
        label: "申请单号",
        tooltip: true,
      },
      {
        prop: "applicationProject",
        label: "医嘱名称",
        tooltip: true,
      },
      {
        prop: "billingDoctor",
        label: "开单医生",
        tooltip: true,
        render: ({ row }) => {
          const patientType = data.istDoctorAsOptions ?? [];
          let resText = row.billingDoctor + "";
          if (patientType && patientType.length > 0) {
            let filter = patientType.filter(
              (item) => item.value == row.billingDoctor
            );
            if (filter && filter.length > 0) {
              resText = filter[0].label;
            }
          }
          return h("div", resText);
        },
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) => query("tableData", pagination),
    onSelectionChange: (data) => {},
    highlightCurrentRow: true,
  };
  return tableProps;
});

const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    insertIndex: false,
    columns: [
      {
        prop: "specimenNumber",
        label: "标本号",
      },
      {
        prop: "patientName",
        label: "姓名",
        tooltip: true,
      },
      {
        prop: "age",
        label: "年龄",
        tooltip: true,
      },
      {
        prop: "sex",
        label: "性别",
        tooltip: true,
      },
      {
        prop: "outpatientNumber",
        label: "门诊号/住院号",
        tooltip: true,
      },
      {
        prop: "bedNumber",
        label: "床位",
        tooltip: true,
        render: ({ row }) => {
          const patientType = data.listAsBedOptions ?? [];
          let resText = row.bedNumber + "";
          if (patientType && patientType.length > 0) {
            let filter = patientType.filter(
              (item) => item.value == row.bedNumber
            );
            if (filter && filter.length > 0) {
              resText = filter[0].label;
            }
          }
          return h("div", resText);
        },
      },
      {
        prop: "billingDepartment",
        label: "开单科室",
        tooltip: true,
        render: ({ row }) => {
          const patientType = data.listAsOptions ?? [];
          let resText = row.billingDepartment + "";
          if (patientType && patientType.length > 0) {
            let filter = patientType.filter(
              (item) => item.value == row.billingDepartment
            );
            if (filter && filter.length > 0) {
              resText = filter[0].label;
            }
          }
          return h("div", resText);
        },
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) => query("tableData", pagination),
    onSelectionChange: (data) => {},
  };
  return tableProps;
});

const onPageActions = (action: IAction, model?: any) => {
  switch (action.event) {
    case "add":
      break;
    case "destroy":
      break;
  }
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      query("tableData");
      query("listDoctorAsOptions");
      query("listAsOptions");
      query("listAsBedOptions");
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.sampleAeeangement {
  height: 100%;
  display: flex;

  &__left {
    flex: 5;
    margin-right: 10px;
  }

  &__center {
    flex: 3;
    margin-right: 10px;
    height: 50%;
  }

  &__right {
    flex: 8;
    height: 50%;
  }
}
</style>
